package model

import (
	"gorm.io/gorm/clause"
	"server/common"
)

type Tool struct {
	Model
	Name  string `json:"name"`
	Desc  string `json:"desc"` //工具描述
	Param string `json:"param"`

	OwnerID int `json:"owner_id" json:"owner_id"`
	Owner   Owner

	ToolMeauID int `gorm:"comuln:tool_meau_id;" json:"tool_meau_id"`
}

func (t *Tool) Create() error {
	if err := common.DB.Create(&t).Error; err != nil {
		return err
	}
	return nil
}

func (t *Tool) List(father int) []Tool {
	var tools_list []Tool
	if err := common.DB.Debug().Model(&ToolMeau{}).Preload(clause.Associations).Where("father = ?", father).Find(&tools_list).Error; err != nil {
		return tools_list
	}
	return tools_list
}

func (t *Tool) Search(name string) []Tool {
	var tools_list []Tool
	if err := common.DB.Debug().Model(&ToolMeau{}).Preload(clause.Associations).Where("name = ?", name).Find(&tools_list).Error; err != nil {
		return tools_list
	}
	return tools_list
}

func (t *Tool) Update() error {
	if err := common.DB.Where("name = ?", t.Name).Updates(&t).Error; err != nil {
		return err
	}
	return nil
}

func (t *Tool) Delete() error {
	if err := common.DB.Where("name = ?", t.Name).Delete(&t).Error; err != nil {
		return err
	}
	return nil
}
